<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\bill;

use app\common\dao\BaseDao;
use app\common\model\bill\BillCatalogModel;

class BillCatalogDao extends BaseDao
{

    /**
     * @return BillCatalogModel
     */
    protected function getModel(): string
    {
        return BillCatalogModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['cate_id']) && $where['cate_id'] !== '', function ($query) use ($where) {
                $query->where('cate_id', intval($where['cate_id']));
            })
            ->when(isset($where['is_type']) && $where['is_type'] !== '', function ($query) use ($where) {
                $query->where('is_type', intval($where['is_type']));
            })
            ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
                if (is_array($where['status'])) {
                    $query->whereIn('status', $where['status']);
                } else {
                    $query->where('status', (int)$where['status']);
                }
            })
            ->when(isset($where['bill_id']) && $where['bill_id'] !== '', function ($query) use ($where) {
                $query->where('bill_id', intval($where['bill_id']));
            })
            ->when(isset($where['room_id']) && $where['room_id'] !== '', function ($query) use ($where) {
                $query->where('room_id', intval($where['room_id']));
            })
            ->when(isset($where['contract_id']) && $where['contract_id'] !== '', function ($query) use ($where) {
                $query->where('contract_id', intval($where['contract_id']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('remark', '%' . trim($where['keywords']) . '%');
            });
    }

    public function deleteByBillId(int $id)
    {
        return $this->getSearch([
            'bill_id' => $id
        ])->delete();
    }

    public function updateByBillId(int $id, int $status)
    {
        return $this->getSearch([
            'bill_id' => $id
        ])->update(['status' => $status]);
    }
}
